﻿using MyBlog.IRepository;
using MyBlog.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyBlog.Service
{
    public class BaseService<TEtntiy> : IBaseService<TEtntiy> where TEtntiy : class, new()
    {
        //从子类的构造函数中传入该仓储对象
        protected IBaseRepository<TEtntiy> _baseRepository;
        public async Task<bool> CreateAsync(TEtntiy entity)
        {
            return await _baseRepository.CreateAsync(entity);
        }

        public async Task<bool> DeleteAsync(int id)
        {
            return await _baseRepository.DeleteAsync(id);
        }

        public async Task<bool> EditAsync(TEtntiy entity)
        {
            return await _baseRepository.EditAsync(entity);
        }

        //写成虚方法，是为了以后的导航查询,子类重写
        public virtual async Task<TEtntiy> FindAsync(int id)
        {
            return await _baseRepository.FindAsync(id);
        }

        public virtual async Task<TEtntiy> FindAsync(Expression<Func<TEtntiy, bool>> func)
        {
            return await _baseRepository.FindAsync(func);
        }

        public virtual async Task<List<TEtntiy>> QueryAsync()
        {
            return await _baseRepository.QueryAsync();
        }

        public virtual async Task<List<TEtntiy>> QueryAsync(Expression<Func<TEtntiy, bool>> func)
        {
            return await _baseRepository.QueryAsync(func);
        }

        public virtual async Task<List<TEtntiy>> QueryAsync(int page, int size, RefAsync<int> total)
        {
            return await _baseRepository.QueryAsync(page, size, total);
        }

        public virtual async Task<List<TEtntiy>> QueryAsync(Expression<Func<TEtntiy, bool>> func, int page, int size, RefAsync<int> total)
        {
            return await _baseRepository.QueryAsync(func, page, size, total);
        }
    }
}
